const screenitemservice = require("../../../../apiservice/screen/screenitemservice.js");
const screenresultservice = require("../../../../apiservice/screen/screenresultservice.js");
const screencustomerservice = require("../../../../apiservice/screen/screencustomerservice.js");
const screenreservationservice = require("../../../../apiservice/screen/screenreservationserive.js");
const helper = require('./helper.js');
const userinfo = wx.getStorageSync('useridentity');
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;

module.exports = Behavior({
	behaviors: [],
	properties: {},
	data: {
    screentype:''
  },
	methods: {
		//获取筛查客户信息
		async GetScreenCustomerBy(scid, customerId) {
			try {
				var sccustomer = await screencustomerservice.GetScreenCustomerByScreenIdAndLeadId(scid, customerId);
				console.log(sccustomer.data);
				var new_scstate;
				if(this.data.screentemplatetype=='2'){
					new_scstate= 1;
				}else if(this.data.screentemplatetype=='1'){
					new_scstate= 0;
				}
				if (sccustomer) {
					var screenitem = this.data.screenitem;
					this.setData({
						screenitemIndex: helper.getPickerValue(screenitem, 'new_screenitemId', sccustomer.data.new_scitemid),
						diseaseIndex: helper.getdiseaseArrayIndex(sccustomer.data.new_disease),
						insuranceTypeIndex: helper.getinsuranceTypeIndex(sccustomer.data.new_insurancetype),
						//筛查客户
						['sccustomer.new_sc_customerid']: sccustomer.data.sc_customerid,
						//潜在客户
						['lead.new_patientsource']: 3,
						['lead.new_qq']: sccustomer.data.qq ? sccustomer.data.qq : '',
						['lead.new_age']: sccustomer.data.age ? sccustomer.data.age : '',
						['lead.leadid']: sccustomer.data.leadid ? sccustomer.data.leadid : '',
						['lead.new_gender']: sccustomer.data.sex ? sccustomer.data.sex : '',
						['lead.new_alias']: sccustomer.data.alias ? sccustomer.data.alias : '',
						['lead.telephone2']: sccustomer.data.telno ? sccustomer.data.telno : '',
						['lead.new_citizenid']: sccustomer.data.sfz ? sccustomer.data.sfz : '',
						['lead.emailaddress1']: sccustomer.data.email ? sccustomer.data.email : '',
						['lead.new_wechat']: sccustomer.data.weixinno ? sccustomer.data.weixinno : '',
						['lead.new_mobile']: sccustomer.data.iphoneno2 ? sccustomer.data.iphoneno2 : '',
						['lead.lastname']: sccustomer.data.customername ? sccustomer.data.customername : '',
						['lead.address1_name']: sccustomer.data.address ? sccustomer.data.address : '',
						['lead.description']: sccustomer.data.remark ? sccustomer.data.remark : '',
						['lead.new_birthdate']: sccustomer.data.birthdate ? sccustomer.data.birthdate : '',
						['lead.address1_telephone2']: sccustomer.data.telno2 ? sccustomer.data.telno2 : '',
						['lead.address1_telephone1']: sccustomer.data.iphoneno1 ? sccustomer.data.iphoneno1 : '',
						['lead.address1_telephone3']: sccustomer.data.parentphone ? sccustomer.data.parentphone : '',
						['lead.new_disease']: sccustomer.data.new_disease ? sccustomer.data.new_disease : null,
						['lead.new_insuranceid']: sccustomer.data.new_insuranceid ? sccustomer.data.new_insuranceid : '',
						['lead.new_insurancetype']: sccustomer.data.new_insurancetype ? sccustomer.data.new_insurancetype : null,
						//筛查客户	
						['sccustomer.new_scstate']: new_scstate,
						['sccustomer.new_scid']: sccustomer.data.new_scid ? sccustomer.data.new_scid : '',
						['sccustomer.new_scnum']: sccustomer.data.new_scnum ? sccustomer.data.new_scnum : '',
						['sccustomer.new_scitemid']: sccustomer.data.new_scitemid ? sccustomer.data.new_scitemid : '',
						['sccustomer.new_customerid']: sccustomer.data.new_customerid ? sccustomer.data.new_customerid : '',
						['sccustomer.new_screenteamid']: sccustomer.data.new_screenteamid ? sccustomer.data.new_screenteamid : '',
					});
				}
			} catch (e) {
				console.log(e);
			} finally {
				wx.hideLoading();
			}
		},
		//获取筛查结果
		async GetScreenResultBy(scid, customerId) {
			try {
				var that = this;
				var scresult = await screenresultservice.GetScreenResultsCode(scid, customerId);
				console.log(scresult)
				scresult.data.findIndex(function(item, index) {
					if (item.new_categoryid == that.data.nakedEyeLeft.new_categoryid &&
						item.new_itemid == that.data.nakedEyeLeft.new_itemid &&
						item.new_contentid == that.data.nakedEyeLeft.new_contentid) {
						that.setData({
							nakedEyeLeftText: item.new_result ? item.new_result : "请填写",
							['nakedEyeLeft.new_result']: item.new_result ? item.new_result : "",
						});
					} else if (item.new_categoryid == that.data.nakedEyeRight.new_categoryid &&
						item.new_itemid == that.data.nakedEyeRight.new_itemid &&
						item.new_contentid == that.data.nakedEyeRight.new_contentid) {

						that.setData({
							nakedEyeRightText: item.new_result ? item.new_result : "请填写",
							['nakedEyeRight.new_result']: item.new_result ? item.new_result : "",
						});
					} else if (item.new_categoryid == that.data.correctRight.new_categoryid &&
						item.new_itemid == that.data.correctRight.new_itemid &&
						item.new_contentid == that.data.correctRight.new_contentid) {
						that.setData({
							correctRightText: item.new_result ? item.new_result : "请填写",
							['correctRight.new_result']: item.new_result ? item.new_result : "",
						});
					} else if (item.new_categoryid == that.data.correctLeft.new_categoryid &&
						item.new_itemid == that.data.correctLeft.new_itemid &&
						item.new_contentid == that.data.correctLeft.new_contentid) {
						that.setData({
							correctLeftText: item.new_result ? item.new_result : "请填写",
							['correctLeft.new_result']: item.new_result ? item.new_result : "",
						});
					}
					//裸眼视力手术情况 右眼
					else if (item.new_categoryid == that.data.nakedEyeRightRes.new_categoryid &&
						item.new_itemid == that.data.nakedEyeRightRes.new_itemid &&
						item.new_contentid == that.data.nakedEyeRightRes.new_contentid) {
						//debugger					
						if (item.new_result == that.data.nakedEyeRightRes.new_result) {
							that.setData({
								RightnakedEyeActive: 1,
							});
						} else {
							that.setData({
								RightnakedEyeActive: 0,
							});
						}

					}
					//裸眼视力手术情况 左眼
					else if (item.new_categoryid == that.data.nakedEyeLeftRes.new_categoryid &&
						item.new_itemid == that.data.nakedEyeLeftRes.new_itemid &&
						item.new_contentid == that.data.nakedEyeLeftRes.new_contentid) {
						if (item.new_result == that.data.nakedEyeLeftRes.new_result) {
							that.setData({
								LeftnakedEyeActive: 1,
							});
						} else {
							that.setData({
								LeftnakedEyeActive: 0,
							});
						}

					} else if (item.new_categoryid == that.data.systemicDiseases.new_categoryid &&
						item.new_itemid == that.data.systemicDiseases.new_itemid &&
						item.new_contentid == that.data.systemicDiseases.new_contentid) {
							//debugger;
						that.setData({
							result: item.new_result.split(","),
							['systemicDiseases.new_result']: item.new_result,
						});
					} else if (item.new_categoryid == that.data.cataractRight.new_categoryid &&
						item.new_itemid == that.data.cataractRight.new_itemid &&
						item.new_contentid == that.data.cataractRight.new_contentid) {
						let catart = that.data.cataractResult.filter((val, index) => {
							return val.new_result == item.new_result;
						});
						that.setData({
							['cataractRight.new_result']: item.new_result,
							['cataractRight.active']: catart[0].result_Index,
						});
					} else if (item.new_categoryid == that.data.cataractLeft.new_categoryid &&
						item.new_itemid == that.data.cataractLeft.new_itemid &&
						item.new_contentid == that.data.cataractLeft.new_contentid) {
						let catart = that.data.cataractResult.filter((val, index) => {
							return val.new_result == item.new_result;
						});
						that.setData({
							['cataractLeft.new_result']: item.new_result,
							['cataractLeft.active']: catart[0].result_Index,
						});
					} else if (item.new_categoryid == that.data.pterygiumRight.new_categoryid &&
						item.new_itemid == that.data.pterygiumRight.new_itemid &&
						item.new_contentid == that.data.pterygiumRight.new_contentid) {
						let catart = that.data.pterygiumResult.filter((val, index) => {
							return val.new_result == item.new_result;
						});

						that.setData({
							['pterygiumRight.new_result']: item.new_result,
							['pterygiumRight.active']: catart[0].result_Index,
						});
					} else if (item.new_categoryid == that.data.pterygiumLeft.new_categoryid &&
						item.new_itemid == that.data.pterygiumLeft.new_itemid &&
						item.new_contentid == that.data.pterygiumLeft.new_contentid) {
						let catart = that.data.pterygiumResult.filter((val, index) => {
							return val.new_result == item.new_result;
						});
						that.setData({
							['pterygiumLeft.new_result']: item.new_result,
							['pterygiumLeft.active']: catart[0].result_Index,
						});
					} else if (item.new_categoryid == that.data.lacrimalObstruction.new_categoryid &&
						item.new_itemid == that.data.lacrimalObstruction.new_itemid &&
						item.new_contentid == that.data.lacrimalObstruction.new_contentid) {
						let catart = that.data.lacrimalObstructionResult.filter((val, index) => {
							return val.new_result == item.new_result;
						});
						that.setData({
							['lacrimalObstruction.new_result']: item.new_result,
							['lacrimalObstruction.active']: catart[0].result_Index,
						});
					} else if (item.new_categoryid == that.data.trichiasis.new_categoryid &&
						item.new_itemid == that.data.trichiasis.new_itemid &&
						item.new_contentid == that.data.trichiasis.new_contentid) {
						let catart = that.data.lacrimalObstructionResult.filter((val, index) => {
							return val.new_result == item.new_result;
						});
						that.setData({
							['trichiasis.new_result']: item.new_result,
							['trichiasis.active']: catart[0].result_Index,
						});
					} else if (item.new_categoryid == that.data.conjunctivitis.new_categoryid &&
						item.new_itemid == that.data.conjunctivitis.new_itemid &&
						item.new_contentid == that.data.conjunctivitis.new_contentid) {
						let catart = that.data.lacrimalObstructionResult.filter((val, index) => {
							return val.new_result == item.new_result;
						});
						that.setData({
							['conjunctivitis.new_result']: item.new_result,
							['conjunctivitis.active']: catart[0].result_Index,
						});
					} else if (item.new_categoryid == that.data.ametropia.new_categoryid &&
						item.new_itemid == that.data.ametropia.new_itemid &&
						item.new_contentid == that.data.ametropia.new_contentid) {
						let catart = that.data.lacrimalObstructionResult.filter((val, index) => {
							return val.new_result == item.new_result;
						});
						that.setData({
							['ametropia.new_result']: item.new_result,
							['ametropia.active']: catart[0].result_Index,
						});
					} else if (item.new_categoryid == that.data.fundusDiseases.new_categoryid &&
						item.new_itemid == that.data.fundusDiseases.new_itemid &&
						item.new_contentid == that.data.fundusDiseases.new_contentid) {
						let catart = that.data.lacrimalObstructionResult.filter((val, index) => {
							return val.new_result == item.new_result;
						});
						that.setData({
							['fundusDiseases.new_result']: item.new_result,
							['fundusDiseases.active']: catart[0].result_Index,
						});
					} else if (item.new_categoryid == that.data.descriptions.new_categoryid &&
						item.new_itemid == that.data.descriptions.new_itemid &&
						item.new_contentid == that.data.descriptions.new_contentid) {
						that.setData({
							['descriptions.new_result']: item.new_result ? item.new_result : '',
						});
					} else if (item.new_categoryid == that.data.advise.new_categoryid &&
						item.new_itemid == that.data.advise.new_itemid &&
						item.new_contentid == that.data.advise.new_contentid) {
						that.setData({
							['advise.new_result']: item.new_result ? item.new_result : '',
						});
					}
				})
			} catch (e) {
				console.log(e);
			} finally {
				//wx.hideLoading();
			}
		},
		//获取预约记录信息
		async GetReservation(leadId, scId) {
			var reservation = await screenreservationservice.QueryReservationByLeadIdAndScid(leadId, scId);
			console.log(reservation);
			if (reservation.data.length > 0) {
				var result = reservation.data[0];
				console.log(result);
				await this.GetDempartmentByHospital(result.new_hospital);
				// debuggerdebugger
				console.log(this.data.departmentList);
				var time = new Date(result.new_reservationdate);
				time.setHours(time.getHours() + 8)
				var new_reservationdate = util.frmatDate(time);
				this.setData({
					reservationShow: true,
					iSreservation: 0,
					['reservation.new_reservationdate']: new_reservationdate,
					['reservation.new_reservationid']: result.new_reservationId,
					hospitalindex: helper.getPickerValue(this.data.hospitalList, 'siteId', result.new_hospital),
					departmentIndex: helper.getPickerValue(this.data.departmentList, 'new_departmentId', result.new_department)
				})
			}
		},
		//根据当前用户所在的医院获取筛查项目
		async GetScreenItem(hospitalId) {
			try {
				var screenitems = await screenitemservice.GetScreenItemData(hospitalId);
				if (screenitems) {
					this.setdata({
						screenitem: screenitems
					});
				}
			} catch (e) {
				console.log(e);
			} finally {
				//wx.hideLoading();
			}
		},
		//获取所有医院
		async GetAllHospital() {
			try {
				var hospitals = await screenitemservice.GetAllHospital();
				var hospitalIndex = hospitals.data.findIndex(function(item, index) {
					return item.siteId == userinfo.siteid;
				});
				if (hospitals) {
					this.setData({
						hospitalList: hospitals.data,
						hospitalindex: hospitalIndex,
					});
				}
			} catch (e) {
				console.log(e);
			} finally {
				//wx.hideLoading();
			}
		},
		//获取科室信息
		async GetDempartmentByHospital(siteid) {
			try {
				var departments = await screencustomerservice.GetDepartmentByHospitalId(siteid);
				// debugger
				if (departments) {
					this.setData({
						departmentList: departments.data,
					})
					console.log(departments);
				}
			} catch (e) {
				console.log(e);
			} finally {
				//wx.hideLoading();
			}
		},
		//获取筛查项目
		async GetAllScreenItems() {
			try {
				var that = this;
				var screemitems = await screenitemservice.GetScreenAllTeams();
				if (screemitems) {
					var list = that.data.screenitem.concat(screemitems.data);
					that.setData({
						screenitem: list
					});
				}
			} catch (e) {
				console.log(e);
			} finally {
				//wx.hideLoading();
			}
		},
		//设置需要创建的筛查结果
		SetResultToArray: function() {
			var scResults = [];
			console.log(this.data.result);
			if (this.data.result != '') {
				var disresult = '';
				var systemicDiseases = this.data.systemicDiseases; //筛查结果 全身疾病史
				this.data.result.filter((val, index) => {
					if (val != '') {
						disresult += val + ',';
					}
				});
				systemicDiseases.new_result = disresult.substr(0, disresult.length - 1),
					scResults.push(systemicDiseases);
			}
      // 中小学校园 不能修改，社区、大客户、大学校园不需要判断结果
			// if(this.data.nakedEyeLeft.new_result=='无光感'||this.data.nakedEyeLeft.new_result=='手动'||this.data.nakedEyeLeft.new_result=='指数'
			// ||this.data.nakedEyeRight.new_result=='光感'||this.data.nakedEyeLeft.new_result=='光感'
			// ||this.data.nakedEyeRight.new_result=='无光感'||this.data.nakedEyeRight.new_result=='手动'||this.data.nakedEyeRight.new_result=='指数'){
			// 	this.data.eyeResult.new_result = 'SC0000000005';
			// }else{
			// 	var signLeft = parseFloat(this.data.nakedEyeLeft.new_result);
			// 	var signRight = parseFloat(this.data.nakedEyeRight.new_result);
			// 	if (signLeft < 4.9 || signRight < 4.9) {
			// 		this.data.eyeResult.new_result = 'SC0000000005';
			// 	}
			// }			
			// scResults.push(this.data.eyeResult);

			scResults.push(this.data.nakedEyeRight);
			scResults.push(this.data.nakedEyeLeft);
			scResults.push(this.data.correctRight);
			scResults.push(this.data.correctLeft);
			scResults.push(this.data.nakedEyeRightRes);
			scResults.push(this.data.nakedEyeLeftRes);
			scResults.push(this.data.cataractRight);
			scResults.push(this.data.cataractLeft);
			scResults.push(this.data.pterygiumRight);
			scResults.push(this.data.pterygiumLeft);
			scResults.push(this.data.lacrimalObstruction);
			scResults.push(this.data.trichiasis);
			scResults.push(this.data.conjunctivitis);
			scResults.push(this.data.ametropia);
			scResults.push(this.data.fundusDiseases);
			if (this.data.descriptions.new_result.replace(/\s+/g, '') != '') {
				scResults.push(this.data.descriptions)
			}
			if (this.data.advise.new_result.replace(/\s+/g, '') != '') {
				scResults.push(this.data.advise)
			}

			return scResults;
		},
		//更新社区筛查单人模式录入
		UpdataSQSC: async function(obj) {
			var that = this;
			wx.showLoading({
				title: '提交中',
				mask: true
			})
			try {
				var result = await screenitemservice.UpdateSQSC(obj);
        
				this.showResult(result.data);
			} catch (e) {
				//TODO handle the exception
			} finally {
				wx.hideLoading();
			}

		},
		//显示筛查结果
		showResult: function(res) {
			var title = '';
			var icon = '';
			if (res.resultState == 1) {
				title = '修改成功';
				icon = 'success';
				this.AccountListCallbackEvent();
			} else {
				title = '修改失败';
				icon = 'none';
			}
			wx.showToast({
				mask: true,
				title: title,
				icon: icon,
				duration: 2000
			});
		},
		//回调
		AccountListCallbackEvent: function() {
			var that = this;
			let eventChannel = that.getOpenerEventChannel();
			let leadObj = {
				name: that.data.lead.lastname,
				new_age: that.data.lead.new_age,
				new_citizenid: that.data.lead.new_citizenid,
				address1_telephone1: that.data.lead.address1_telephone1,
			};
			eventChannel.emit('ResultModifyEvent', {
				data: leadObj,
			})
		},
	},
});
